<?php 
namespace App\Http\Controllers\Home;
use Session;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Home\ThrottlesLogins;
use Illuminate\Foundation\Home\AuthenticatesAndRegistersUsers;
use DB;
header('content-type:text/html;charset=utf8');

class CateController extends Controller{
	public function category(){
		return view('Home/category');
	}
	public function categorylist($cate_id=""){
		// echo $cate_id;
		$goods_Home= new \App\goods_Home();
		$cate_Home= new \App\cate_Home();
		$cate_one=$cate_Home->lists(1);
		$cate_two=$cate_Home->lists(2);
		$cate_three=$cate_Home->lists(3);
		if(empty($cate_id)){
			$res=$goods_Home->listss();
		}else{
			$res=$goods_Home->lists($cate_id);
		}
		$category = session::get('category');
		// print_r($category);
		return view('Home/categorylist',['res'=>$res,'ak'=>$category,'class'=>'now','cate_one'=>$cate_one,'cate_two'=>$cate_two,'cate_three'=>$cate_three,'cate_id'=>$cate_id]);
	}
	public function category_hot(){
		$goods_Home= new \App\goods_Home();
		$cate_Home= new \App\cate_Home();
		$cate_one=$cate_Home->lists(1);
		$cate_two=$cate_Home->lists(2);
		$cate_three=$cate_Home->lists(3);
		$res=$goods_Home->list_hot();
		// echo "<pre>";
  //       print_r($res);
		$category = session::get('category');
		return view('Home/categorylist',['res'=>$res,'ak'=>$category,'class'=>'now','cate_one'=>$cate_one,'cate_two'=>$cate_two,'cate_three'=>$cate_three,'cate_id'=>""]);
	}
	public function category_new(){
		$goods_Home= new \App\goods_Home();
		$cate_Home= new \App\cate_Home();
		$cate_one=$cate_Home->lists(1);
		$cate_two=$cate_Home->lists(2);
		$cate_three=$cate_Home->lists(3);
		$res=$goods_Home->list_new();
		$category = session::get('category');
		return view('Home/categorylist',['res'=>$res,'ak'=>$category,'class'=>'now','cate_one'=>$cate_one,'cate_two'=>$cate_two,'cate_three'=>$cate_three,'cate_id'=>""]);
	}

	//获取商品详情
	public function product($goods_id){
		
		$model = new \App\Goods_Home();
		///获取商品详情
		$info=$model->getGoodsData($goods_id);
		$Goodsinfo = array_map('get_object_vars', $info);
		
		//获取商品的规格参数
		$spec=$model->getGoodsSpec($goods_id);
		$spec = array_map('get_object_vars', $spec);

		$attr_id=array_unique(array_column($spec,'attr_id'));

		$specList=$this->get_where_string($attr_id,'attr_id');

		
		//合并数组 参数规格
		foreach($spec as $v){
		    $specList[$v['attr_id']]['spec'][] = $v;
		}
		// print_r($specList);die;
		//获取默认选中的规格参数
		$aotuChecked=$this->getAutoSpec($model,$goods_id);
		if(!empty($aotuChecked)){
			$attr=$aotuChecked->product_attr;
			$category = session::get('category');
			return view('Home/product',['Goodsinfo'=>$Goodsinfo[0],'specList'=>$specList,'attr'=>$attr,'ak'=>$category]);
		}else{
			echo '<script>alert("该商品没有货品");history.go(-1)</script>';
		}
	}

	//检测所选的属性下是否有可选属性 更新价钱
	public function checkAttr($goods_id,$product_attr){
		$model = new \App\Goods_Home();
		$product=$model->checkattr($goods_id,$product_attr);
		$product = array_map('get_object_vars', $product);
		if(!empty($product)){
			$product=array_column($product,'product_attr');
			echo json_encode($product);
		}else{
			echo 2;
		}
	}

	//默认选中的商品规格
	public function getAutoSpec($model,$goods_id){
		// print_r($goods_id);;
		return $aotuChecked=$model->getAutoOne($goods_id);
	}


	//添加到购物车
	public function addCart($goods_id,$product_attr,$buy_nums){
		$model = new \App\Goods_Home();
		if(Session::has('user_id') && Session::has('user_name')){
			//验证库存是否足够
			$res=$this->checkNumber($goods_id,$product_attr,$buy_nums);
	
			if(is_int($res)){
				return $res;
			}
			//查询商品信息
			$goodsOne=$this->getGoodsInfo($goods_id);
			$goodsOne=array_map('get_object_vars', $goodsOne);

			//如果当前商品存在货品 查询货品信息 where goods_id product_attr 根据product_attr(商品属性id)
			//去商品属性表(goods_attr)里查询对应的值 attr_value;
			if($product_attr!=''){
				$product=$this->getProduct($goods_id,$product_attr);
			}
			//组装购物车数据
			$cartData=$this->setCartData($goodsOne[0],$product,$buy_nums);
			$cartData['uid']=session::get('user_id');
			//判断当前用户  当前属性规格添加到购物车了没 如果添加过则不让在添加 where uid goods_id product_attr 
			$userAttr=$model->checkUserAttr($cartData['uid'],$goods_id,$product_attr);
			if(empty($userAttr)){
				//为空 则可以添加
				$res=$model->addOneCart($cartData);
				if($res){
					echo json_encode($cartData);
				}
			}else{
				//提示已经有此规格的商品
				return 4;
			}
	
		}else{
			echo 1;//没登录 请先登录
		}
	}

	//组装购物车数据
	public function setCartData($goodsOne,$product,$buy_nums){
		
		$cartData = array(
		    'goods_id'       => $goodsOne['goods_id'],
		    'goods_name'     => $goodsOne['goods_name'],
		    'goods_sn'       => $goodsOne['goods_sn'],
		    'goods_price'    => $goodsOne['shop_price']+$product['product_attr_price'],//商品本店单价+规格的价格= 购买的商品价格
		    'goods_price_one'    => $goodsOne['shop_price'],//商品本店单价
		    'product_attr_price'    =>empty($product)?'':$product['product_attr_price'],//规格的价格
		    'buy_num'     => $buy_nums,
		    'goods_img'      => $goodsOne['goods_img'],
		    'product_attr_value'    =>empty($product)?'':$product['product_attr_value'],//规格的值
		    'product_attr_id'       =>empty($product)?'':$product['product_attr_id'],//规格的id
		    'status'       =>0
		);
		if(!empty($product)){
		    $cartData['goods_sum_price'] = $cartData['goods_price'] * $cartData['buy_num'];
		}
		return $cartData;
	}

	//获取货品数据
	public function getProduct($goods_id,$product_attr){
		$list=$this->get_where_strings(explode('|',$product_attr), 'goods_attr_id');

		//属性的value
		$product['product_attr_value']=$product_attr_value = implode('|',array_column($list,'attr_value'));
		
		//属性的总价
		$product['product_attr_price']=$product_price=array_sum(array_column($list, 'attr_price'));
		//属性的id;
		$product['product_attr_id']=$product_attr;
		return $product;
			
	}

	//验证库存是否足够
	public function checkNumber($goods_id,$product_attr,$buy_nums){
		$model = new \App\Goods_Home();
		//判断当前用户选择的是否有规格 然后判断当前选择的规格库存是否足够
		if(!empty($product_attr)){
			$getone=$model->checkNumber($goods_id,$product_attr,$buy_nums);
			$getone=array_map('get_object_vars', $getone);
			if(empty($getone[0])){
				return 2;//货品库存不足
			}
		}else{
			//没有选择规格 说明只添加了商品和数量 去商品表里查询库存足够
			$getones=$model->checkGoodsNumber($goods_id,$buy_nums);
			$getones=array_map('get_object_vars', $getones);
			if(empty($getones[0])){
				return 3;//商品库存不足
			}
		}
	}


	//获取商品的信息
	public function getGoodsInfo($goods_id){
		$model = new \App\Goods_Home();
		return $model->getGoodsData($goods_id);
	}

	//拼接or条件 attr
	function get_where_string($arr, $key){
			$res = [];
			$query = DB::table('attr');
			if (is_array($arr) && !empty($arr)) 
			{
				foreach ($arr as $k => $value) {
					$singleArr[$key]=$value;

					$query = $query->orWhere($singleArr);
				}
			}
			$arrs =$query->get();

			$result = [];
			foreach ($arrs as $key => $value) {
				$result[$value->attr_id]= [
					'attr_id' => $value->attr_id,
					'attr_name' => $value->attr_name
				];
			}
			return $result;
		}

		//拼接or条件 goods_attr
		function get_where_strings($arr, $key){
				$res = [];
				$query = DB::table('goods_attr');
				if (is_array($arr) && !empty($arr)) 
				{
					foreach ($arr as $k => $value) {
						$singleArr[$key]=$value;

						$query = $query->orWhere($singleArr);
					}
				}
				$arrs =$query->get();
				// print_r($arrs);die;

				$result = [];
				foreach ($arrs as $key => $value) {
					$result[]= [
						'attr_value' => $value->attr_value,
						'attr_price' => $value->attr_price,
					];
				}
				return $result;
			}


}
